/**
 * @Title: AdminController
 * @Package cn.com.yiqi.wsc.portal.controller.admin
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @author 会员系统组-胥钰淋
 * @date 2017/2/8 17:06
 * @version V1.0
 */
package cn.com.yiqi.wsc.portal.controller.admin;

import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.entity.org.OrgEntity;
import cn.com.yiqi.wsc.entity.user.User;
import cn.com.yiqi.wsc.portal.controller.BaseController;
import cn.com.yiqi.wsc.service.org.OrgService;
import cn.com.yiqi.wsc.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 *
 *@ClassName: AdminController
 * @Description: TODO
 * @author 会员系统组-胥钰淋
 * @date 2017/2/8 17:06
 *
 */
@Controller
public class AdminController extends BaseController {

    @Autowired
    private OrgService orgService;

    @RequestMapping("/")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("admin/index");
        this.getUserData(mv);
        return mv;
    }

    @RequestMapping("/user/profile")
    public ModelAndView profile() {
        ModelAndView mv = new ModelAndView("admin/profile");
        this.getUserData(mv);
        return mv;
    }

    @RequestMapping("/user/changePassword")
    public ModelAndView changePassword() {
        ModelAndView mv = new ModelAndView("admin/changePassword");
        this.getUserData(mv);
        return mv;
    }

    @RequestMapping(value = "/user/get", method = RequestMethod.GET)
    @ResponseBody
    public Result getUser() {
        Result result = new Result();
        User user = userService.getCurrentUser();
        if (null == user) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("用户未找到");
            return result;
        }
        result.putData("user", user);

        result.setStatus(ResultStatusEnum.操作成功.getCode());
        return result;
    }

    @RequestMapping(value = "/user/getOrg", method = RequestMethod.GET)
    @ResponseBody
    public Result getUserOrg() {
        Result result = new Result();
        User user = userService.getCurrentUser();
        if (null == user) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("用户未找到");
            return result;
        }

        if (null == user.getOrgId()) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("用户未分配组织机构");
            return result;
        }

        result = orgService.detail(user.getOrgId());

        return result;
    }

}
